/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package cn.apestar.modules.meibang.consultation.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import cn.apestar.common.config.Global;
import cn.apestar.common.persistence.Page;
import cn.apestar.common.utils.StringUtils;
import cn.apestar.common.web.BaseController;
import cn.apestar.modules.meibang.consultation.entity.BeautyConsultationOrderQuestion;
import cn.apestar.modules.meibang.consultation.service.BeautyConsultationOrderAnswerService;
import cn.apestar.modules.meibang.consultation.service.BeautyConsultationOrderQuestionService;

/**
 * questionController
 * @author question
 * @version 2017-06-05
 */
@Controller
@RequestMapping(value = "${adminPath}/consultation/question")
public class BeautyConsultationOrderQuestionController extends BaseController {

    @Autowired
    private BeautyConsultationOrderQuestionService beautyConsultationOrderQuestionService;

    @Autowired
    private BeautyConsultationOrderAnswerService beautyConsultationOrderAnswerService;

    @ModelAttribute
    public BeautyConsultationOrderQuestion get(@RequestParam(required = false) String id) {

        if (id != null && id.length() > 0) {

            id = id.substring(1);
        }

        BeautyConsultationOrderQuestion entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = beautyConsultationOrderQuestionService.get(id);
        }
        if (entity == null) {
            entity = new BeautyConsultationOrderQuestion();
        }
        return entity;
    }

    @RequestMapping(value = {"list" , ""})
    public String list(BeautyConsultationOrderQuestion beautyConsultationOrderQuestion, HttpServletRequest request, HttpServletResponse response,
        Model model) {
        Page<BeautyConsultationOrderQuestion> page = beautyConsultationOrderQuestionService.findPage(new Page<BeautyConsultationOrderQuestion>(
            request, response), beautyConsultationOrderQuestion);
        model.addAttribute("page", page);
        return "modules/meibang/consultation/beautyConsultationOrderQuestionList";

    }

    @RequestMapping(value = {"questionList"})
    public String questionList(BeautyConsultationOrderQuestion beautyConsultationOrderQuestion, HttpServletRequest request,
        HttpServletResponse response, Model model) {

        System.out.println(222);

        List<BeautyConsultationOrderQuestion> qAlist = beautyConsultationOrderQuestionService.findQAList(beautyConsultationOrderQuestion);

        List<BeautyConsultationOrderQuestion> qlist = beautyConsultationOrderQuestionService.findList(beautyConsultationOrderQuestion);

        for (BeautyConsultationOrderQuestion bq : qAlist) {

            bq.setId("A" + bq.getId());

            bq.setpId("Q" + bq.getpId());
            // bq.setAnswerTitle(bq.getQuestionTitle());

        }

        for (BeautyConsultationOrderQuestion bq : qlist) {

            bq.setId("Q" + bq.getId());

            bq.setpId("0");
            bq.setAnswerTitle(bq.getQuestionTitle());

            if ("single-text".equals(bq.getAnswerType())) {

                bq.setAnswerTypeName("单选-文字");

            } else if ("single-image".equals(bq.getAnswerType())) {

                bq.setAnswerTypeName("单选-图片");

            } else if ("single-image-text".equals(bq.getAnswerType())) {

                bq.setAnswerTypeName("单选-图文");

            } else if ("mult-text".equals(bq.getAnswerType())) {

                bq.setAnswerTypeName("多选-文字");

            } else if ("mult-image-text".equals(bq.getAnswerType())) {

                bq.setAnswerTypeName("多选-图文");

            } else if ("blank".equals(bq.getAnswerType())) {

                bq.setAnswerTypeName("填空");

            } else if ("mult-price".equals(bq.getAnswerType())) {

                bq.setAnswerTypeName("多选-价格");

            }

        }

        qAlist.addAll(qlist);

        model.addAttribute("list", qAlist);
        model.addAttribute("orderId", beautyConsultationOrderQuestion.getOrderId());
        return "modules/meibang/consultation/beautyConsultationOrderQuestionList";

    }

    @RequestMapping(value = "form")
    public String form(BeautyConsultationOrderQuestion beautyConsultationOrderQuestion, Model model) {

        if (beautyConsultationOrderQuestion.getId() != null && beautyConsultationOrderQuestion.getId().length() > 0) {

            beautyConsultationOrderQuestion.setId(beautyConsultationOrderQuestion.getId().substring(1));
        }

        System.out.println(beautyConsultationOrderQuestion.getId());

        model.addAttribute("beautyConsultationOrderQuestion", beautyConsultationOrderQuestion);
        return "modules/meibang/consultation/beautyConsultationOrderQuestionForm";
    }

    @RequestMapping(value = "save")
    public String save(BeautyConsultationOrderQuestion beautyConsultationOrderQuestion, Model model, RedirectAttributes redirectAttributes) {
        if (!beanValidator(model, beautyConsultationOrderQuestion)) {
            return form(beautyConsultationOrderQuestion, model);
        }
        beautyConsultationOrderQuestionService.save(beautyConsultationOrderQuestion);
        addMessage(redirectAttributes, "保存成功");
        return "redirect:" + Global.getAdminPath() + "/consultation/question/questionList?repage";
    }

    @RequestMapping(value = "updateImage")
    public String updateImage(BeautyConsultationOrderQuestion bq, RedirectAttributes redirectAttributes) {

        beautyConsultationOrderQuestionService.updateImage(bq);

        addMessage(redirectAttributes, "保存成功");

        return "redirect:" + Global.getAdminPath() + "/consultation?repage";
    }

    @RequestMapping(value = "delete")
    public String delete(BeautyConsultationOrderQuestion beautyConsultationOrderQuestion, RedirectAttributes redirectAttributes) {
        beautyConsultationOrderQuestionService.delete(beautyConsultationOrderQuestion);
        addMessage(redirectAttributes, "删除成功");
        return "redirect:" + Global.getAdminPath() + "/consultation/question/?repage";
    }

}